<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once ('Entity/Skill_Video_Comments_Entity.php');
class Skill_Video_Comments_Model extends Skill_Video_Comments_Entity
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function newVideoComment()
    {
        $sql = "SELECT mn.alias as malias, v.id as vid, v.title as vtitle, 
                v.url_thumbnail, v.alias as valias, c.*
                FROM skill_video_comments c
                INNER JOIN skill_videos v ON v.id = c.vid
                INNER JOIN skill_categories cs ON cs.id = v.cid
                INNER JOIN skill_menus mn ON cs.mid = mn.id
                WHERE c.`status` = 1
                GROUP BY v.id
                ORDER BY c.created DESC
                LIMIT 5"; 
        $query = $this->db->query($sql);
        return $query->result();         
    }
    
    public function getComment_byVid($vid = 0, $ofset = 0, $limit = 10) 
    {
        $sql = "SELECT c.*
            FROM skill_video_comments c
            WHERE c.`status` = 1 AND c.vid = $vid
            ORDER BY c.created DESC
            LIMIT $ofset, $limit";
        $query = $this->db->query($sql);
        return $query->result();
    }
    public function cTotal($vid) 
    {
        $sql = "SELECT c.id
            FROM skill_video_comments c
            WHERE c.`status` = 1 AND c.vid = $vid";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }
    public function addComment()
    {
        $this->setFullname($this->input->post('fullname'));
        $this->setEmail($this->input->post('email'));
        $this->setContents($this->input->post('contents'));
        $this->setVid($this->input->post('vid'));
        $this->setCreated(time());
        $this->setChanged(time());
        $this->setStatus(0);
        return $this->save();
    }

    public function dataSort($field, $order)
    {
        $xOrder = $order == 'asc' ? 'desc' : 'asc';
        $img = $order == 'desc' ? '<img src=\'/data/admin/down.jpg\' />' : ' <img src=\'/data/admin/up.jpg\' />';
        $url = anchor('skill_video_comments/sort/' . $field . '/' . $xOrder, t($field) . $img);
        $_SESSION['sm_sort_skill_video_comments'] = array(
            'field' => $field,
            'order' => $order,
            'url' => $url);
    }

    public function dataIndex($target)
    {
        $numshow = 25;
        $this->db->select('skill_video_comments.id, skill_video_comments.fullname, skill_video_comments.email, skill_video_comments.contents, skill_video_comments.status, skill_video_comments.created, skill_videos.title as vid_type');
        $this->db->from('skill_video_comments');

        $this->db->join('skill_videos', 'skill_video_comments.vid = skill_videos.id');

        if ($this->session->userdata('sm_filter_skill_videos'))
            $this->db->where('skill_video_comments.vid', $this->session->userdata('sm_filter_skill_videos'));
        if ($this->session->userdata('sm_find_skill_video_comments')) {
            $this->db->like('skill_video_comments.title', $this->session->userdata('sm_find_skill_video_comments'));
        }
        if (isset($_SESSION['sm_sort_skill_video_comments'])) {
            $sort = $_SESSION['sm_sort_skill_video_comments'];
            $this->db->order_by('skill_video_comments.' . $sort['field'], $sort['order']);
        } else {
            $this->db->order_by('skill_video_comments.created', 'desc');
        }
        $this->db->limit($numshow, $target);
        $query = $this->db->get();
        $paging = paging_admin('skill_video_comments/index', $this->_total(), $numshow, 4);
        return array(
            'query' => $query,
            'paging' => $paging,
            'fil_skill_videos' => $this->Filter_skill_videos(),
            'formFind' => $this->formFind());
    }

    private function cbo_skill_videos($id = 0)
    {
        $this->load->helper("form");
        $query = $this->db->get("skill_videos");
        foreach ($query->result() as $row) {
            $options[$row->id] = $row->title;
        }
        $sl = $id == 0 ? set_value("vid") : $id;
        //return form_dropdown("vid", $options, $sl);
        return $row->title;
    }

    private function _total()
    {
        return $this->db->count_all_results("skill_video_comments");
    }

    private function _checkskill_video_commentsById($id)
    {
        $query = $this->db->get_where("skill_video_comments", array("id" => $id));
        if ($query->num_rows() > 0)
            return true;
        else {
            notify_set_messages(notify_atten(t("Record không tồn tại")), "skill_video_comments");
            return false;
        }
    }

    private function formFind()
    {
        $this->load->helper('form');
        $attributes = array('name' => 'frmFind', 'id' => 'frmFind');
        $f = form_open(site_url('skill_video_comments/find'), $attributes);
        $data = array(
            'name' => 'txt_find',
            'id' => 'txt_find',
            'value' => '',
            'style' => 'width:200px');
        $f .= form_input($data);
        $s = array(
            'name' => 'submit',
            'value' => t('Find'),
            'class' => 'ibutton green',
            'style' => 'margin-left:10px');
        $f .= form_submit($s);
        if ($this->session->userdata('sm_find_skill_video_comments')) {
            $r = array(
                'name' => 'btnReset',
                'value' => 'Reset',
                'class' => 'ibutton red',
                'style' => 'margin-left:10px');
            $f .= form_submit($r);
        }
        $f .= form_close();
        return $f;
    }

    private function Filter_skill_videos()
    {
        $this->load->helper('form');
        $query = $this->db->get('skill_videos');
        foreach ($query->result() as $row) {
            $options[$row->id] = $row->title;
        }
        $id = !$this->session->userdata('sm_filter_skill_videos') ? 0 : $this->session->userdata('sm_filter_skill_videos');
        $attributes = array('name' => 'frmFilterskill_videos', 'id' => 'frmFilterskill_videos');
        $f = form_open(site_url('skill_video_comments/filterskill_videos'), $attributes);
        $f .= form_dropdown('vid', $options, $id);
        $data = array(
            'name' => 'btnFilterskill_videos',
            'class' => 'ibutton green',
            'style' => 'margin-left:10px',
            'value' => t('Filter'));
        $f .= form_submit($data);
        if ($this->session->userdata('sm_filter_skill_videos')) {
            $r = array(
                'name' => 'btnReset',
                'class' => 'ibutton red',
                'style' => 'margin-left:10px',
                'value' => 'Reset');
            $f .= form_submit($r);
        }
        $f .= form_close();
        return $f;
    }


    public function dataEdit($id)
    {
        if (!$this->_checkskill_video_commentsById($id))
            return redirect(site_url('skill_video_comments'));
        $query = $this->db->get_where('skill_video_comments', array('id' => $id));
        return array('query' => $query, 'cbo_skill_videos' => $this->cbo_skill_videos($query->row('vid')));
    }

    public function dataAdd()
    {
        return array('cbo_skill_videos' => $this->cbo_skill_videos());
    }

    public function checkForm()
    {
        /* $this->form_validation->set_rules('fullname', t('fullname'), 'trim|required');
        $this->form_validation->set_rules('email', t('email'), 'trim|required');
        $this->form_validation->set_rules('contents', t('contents'), 'trim|required');
        $this->form_validation->set_rules('status', t('status'), 'trim|required|integer');
        $this->form_validation->set_rules('created', t('created'), 'trim|required|integer');
        $this->form_validation->set_rules('vid', t('vid'), 'trim|required|integer'); */
         $this->form_validation->set_rules('status', t('status'), 'trim');
        return $this->form_validation->run();
    }

    public function doUpdate()
    {
        $this->load->helper('text');
        //$this->setFullname($this->input->post('fullname'));
        //$this->setEmail($this->input->post('email'));
        //$this->setContents($this->input->post('contents'));
        $this->setStatus($this->input->post('status'));
        //$this->setCreated($this->input->post('created'));
        //$this->setVid($this->input->post('vid'));

        if (isset($_POST['idx'])) {
            $id = decrypt($_POST['idx']);
            $this->setId($id);
            $this->setChanged(time());
        } else {
            $this->setCreated(time());
        }
        return $this->save();
    }
}
